import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: wujing
 * @Date: 2024/6/26 9:17
 * @Description: TODO
 **/
@Slf4j
public class Refelct {
    public static void main(String[] args) throws Exception {

        Set<Object> sets = new HashSet<>();
        for (int i = 0; i < 10000; i++) {
            sets.add(i);
        }
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        long start = System.currentTimeMillis();
        URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("C:\\self\\develop\\workspace\\CirroDataUDF\\target\\CirroDataUDF.jar").toURI().toURL()});
        Class<?> aClass = classLoader.loadClass("com.unicom.CirroDataUDF");
        Object instance = aClass.newInstance();
        Method method = aClass.getMethod("Pub_Model_Decrypt", String.class, String.class);

        CompletableFuture[] cfs = sets.stream()
                .map(image -> CompletableFuture.runAsync(() ->   extracted(image,method,instance), executorService)
                        /*.thenApply(images -> result.add(images)*/)
                .toArray(CompletableFuture[]::new);
        CompletableFuture.allOf(cfs).join();
        long end = System.currentTimeMillis();
        log.info("总耗时：" + (end - start) + " ms");
       /* ExecutorService executorService = Executors.newFixedThreadPool(200, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                return null;
            }
        });
        CompletableFuture<Object> resutls = CompletableFuture.supplyAsync(() ->
            try {
                URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("C:\\self\\develop\\workspace\\CirroDataUDF\\target\\CirroDataUDF.jar").toURI().toURL()});
                Class<?> aClass = classLoader.loadClass("com.unicom.CirroDataUDF");
                Object instance = aClass.newInstance();
                Method method = aClass.getMethod("Pub_Model_Decrypt", String.class, String.class);
                Object result = method.invoke(instance, "f88240424a1ea57243995defc8b74e5d", "DWD_D_EVT_GC_CBB_SPEED");
                log.info(result.toString());
                return result;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }, executorService);
        CompletableFuture.allOf(resutls).join();
        System.out.println(resutls.get());
*/
        executorService.shutdownNow();
    }

    private static void extracted(Object i, Method method, Object instance)   {
        try {
            int type = Integer.parseInt(String.valueOf(i));
            Object result = "";
            if(  type % 3 == 1){
                result = method.invoke(instance, "f88240424a1ea57243995defc8b74e5d", "DWD_D_EVT_GC_CBB_SPEED");
            }else if(  type % 3 == 2){
                result = method.invoke(instance,"A5E9614971C5388006B28D8BCAC17DBE", "DWD_D_MRT_CCP_MANAGER_SIGN");
            }else {
                result = method.invoke(instance,"+b74WVeX4eC2TAXAQ98YHJoj5t/I8g4MusMhYYVaZzWORrD6k+k2jsMSBe19GtpvmU/FmmNN9t+Pk6KVXazv/BjTqFbS9aadJUCk1miXEREyBNqo9QKY1eyprOgj9KMllX1CYF8T561IRBwnWafEYVqJFPbjzzp0dRaPaTpv4ze30zHlsptCCq/po0vKCQUs5YZmY68gjE0blAip2R4p2FcgvFjXQgTntKCs4QP2HOk+Htk1tbsf5ZnI1JHrJrKK", "SH_D_B_ZF_FQKSMX");
            }
            log.info(result.toString());
        } catch (Exception e) {
            log.error("", e);
        }

    }

    private static void extracted(Object i) {
        try {
            URLClassLoader classLoader = new URLClassLoader(new URL[]{new File("C:\\self\\develop\\workspace\\CirroDataUDF\\target\\CirroDataUDF.jar").toURI().toURL()});
            Class<?> aClass = classLoader.loadClass("com.unicom.CirroDataUDF");
            Object instance = aClass.newInstance();
            Method method = aClass.getMethod("Pub_Model_Decrypt", String.class, String.class);
            int type = Integer.parseInt(String.valueOf(i));
            Object result = null;
            if(  type % 3 == 1){
                result = method.invoke(instance, "f88240424a1ea57243995defc8b74e5d", "DWD_D_EVT_GC_CBB_SPEED");
            }else if(  type % 3 == 2){
                result = method.invoke(instance,"A5E9614971C5388006B28D8BCAC17DBE", "DWD_D_MRT_CCP_MANAGER_SIGN");
            }else {
                result = method.invoke(instance,"+b74WVeX4eC2TAXAQ98YHJoj5t/I8g4MusMhYYVaZzWORrD6k+k2jsMSBe19GtpvmU/FmmNN9t+Pk6KVXazv/BjTqFbS9aadJUCk1miXEREyBNqo9QKY1eyprOgj9KMllX1CYF8T561IRBwnWafEYVqJFPbjzzp0dRaPaTpv4ze30zHlsptCCq/po0vKCQUs5YZmY68gjE0blAip2R4p2FcgvFjXQgTntKCs4QP2HOk+Htk1tbsf5ZnI1JHrJrKK", "SH_D_B_ZF_FQKSMX");
            }
            log.info(result.toString());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


}
